package com.jh.controller;
/**
 * 代号:隐无为 2017：厚溥
 * 文件名：ClassifyController.java
 * 创建人：柯栋
 * 日期：2017年9月21日
 * 修改人：
 * 描述：
 */


import java.util.List;

import org.kd.common.base.controller.BaseController;
import org.kd.utils.JsonReturnData;
import org.kd.utils.PageTable;
import org.kd.utils.WebConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.jh.model.Classify;
import com.jh.service.ClassifyService;

/**
 * 用途：业务模块名称
 */
@Controller
@RequestMapping("/classifyController")
public class ClassifyController extends BaseController {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	ClassifyService classifyService;



	@RequestMapping("/list")
	public String list(ModelMap modelMap) {
		logger.info("==============="+this.getClass().getName()+"==================");
		return "classify/list";
	}

	@RequestMapping("/saveUpdatePage")
	public String saveUpdatePage(Integer id, ModelMap modelMap) {
		if (id != null) {
			modelMap.put("classify", classifyService.queryById(id));
		}
		return "classify/saveUpdatePage";
	}

	@RequestMapping("/saveUpdate")
	@ResponseBody
	public String saveUpdate(Classify classify) {
		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "操作失败");
		if (classify.getId() != null) {
			classifyService.updateNoNull(classify);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("更新成功");
		} else {
			classify.setDelFlag(1);
			classifyService.insert(classify);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("添加成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Classify classify) {

		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "删除失败");
		if (classify.getId() != null) {
			classify.setDelFlag(WebConstant.DELETECODE);
			classifyService.updateDelete(classify);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("删除成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/deletes")
	@ResponseBody
	public String deletes(String ids) {
		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "批量删除失败");
		if (!StringUtils.isEmpty(ids)) {
//			classifyService.updateDeletes(ids);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("批量删除成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/ajaxList")
	@ResponseBody
	public String ajaxList(Integer page, Integer limit) {
		PageTable<List<Classify>> pageTable = new PageTable<List<Classify>>();
		Classify u = new Classify();
		u.setDelFlag(WebConstant.NODELETECODE);
		PageInfo<Classify> pageInfo = classifyService.queryPageListByParam(page, limit, u);
		pageTable.setCode(WebConstant.PAGESUCCESSCODE);
		pageTable.setMsg("获取成功");
		pageTable.setData(pageInfo.getList());
		pageTable.setCount(pageInfo.getTotal());
		return responseAPI.getJsonString(pageTable);
	}
}
